import 'dart:convert';

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lmreader/common/models/splash.dart';

final splashProvider = StateNotifierProvider.autoDispose<SplashViewModel,
        SplashModel>(
    (ref) => SplashViewModel(SplashModel(
        entity: SplashEntity(
            type: SplashType.video,
            eventType: SplashEventType.click,
            sourceUrl:
                "https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4",
            eventAction: "null"))));

class SplashViewModel extends StateNotifier<SplashModel> {
  SplashViewModel(super.state);
}

class SplashModel {
  SplashEntity? entity;
  SplashModel({
    required this.entity,
  });

  SplashModel copyWith({
    SplashEntity? entity,
  }) {
    return SplashModel(
      entity: entity ?? this.entity,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'entity': entity?.toMap(),
    };
  }

  factory SplashModel.fromMap(Map<String, dynamic> map) {
    return SplashModel(
      entity: SplashEntity.fromMap(map['entity']),
    );
  }

  String toJson() => json.encode(toMap());

  factory SplashModel.fromJson(String source) =>
      SplashModel.fromMap(json.decode(source));

  @override
  String toString() => 'SplashModel(entity: $entity)';

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is SplashModel && other.entity == entity;
  }

  @override
  int get hashCode => entity.hashCode;
}
